//
//  CateGoryPage.m
//  HuiPinzhijia
//
//  Created by LLMPro on 16/8/29.
//  Copyright © 2016年 LLMPro. All rights reserved.
//

#import "CateGoryPage.h"

@interface CateGoryPage ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)UITableView *searchPageTableView;
@property (nonatomic, strong)NSMutableArray *sessionArr;
@property (nonatomic, strong)NSMutableArray *cellArr;
@property (nonatomic, assign)BOOL isOpen;
@property (nonatomic, assign)NSInteger section;
@property (nonatomic, strong)UIImageView *scetionImagV;

@end

@implementation CateGoryPage

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.searchPageTableView];
    [self updateData];
}

//伪数据加载
- (void)updateData
{
    for (int i = 0; i<15; i++)
    {
        NSString *sessionStr = [NSString stringWithFormat:@"第%d组",i];
        [self.sessionArr addObject:sessionStr];
        
        NSMutableArray *numCell = [[NSMutableArray alloc]init];
        for (int j = 1; j < 6; j++) {
            NSString *cellStr = [NSString stringWithFormat:@"第%d组第%d行",i,j];
            [numCell addObject:cellStr];
            
        }
        [self.cellArr addObject:numCell];
    }
    [self.searchPageTableView reloadData];
    
}

#pragma mark -- TableView Delegate
#pragma mark 设置分组标题内容高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{
    
    return _sessionArr.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_isOpen == NO)
    {
        return 0;
    }
    
    return ((NSMutableArray *)self.cellArr[section]).count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCell"];
    if (self.cellArr.count <= 0)
    {
        return cell;
    }
    if (!cell)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SCell"];
    }
    
    cell.textLabel.text = ((NSMutableArray *)self.cellArr[indexPath.section])[indexPath.row];
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *nameLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    nameLable.text = self.sessionArr[section];
    [view addSubview:nameLable];
    
    _scetionImagV = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 32, 4, 32, 32)];
    _scetionImagV.image = [UIImage imageNamed:@"guanbi"];
    [view addSubview:_scetionImagV];
    
    UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture)];
    [view addGestureRecognizer:gr];
    
    
    return view;
}

//轻叩时间
- (void)tapGesture
{
    _isOpen = !_isOpen;
    dispatch_async(dispatch_get_main_queue(), ^{
        _scetionImagV.image = [UIImage imageNamed:@"zhankai"];
    });
    
    [self.searchPageTableView reloadData];
}
#pragma mark -- 懒加载
- (UITableView *)searchPageTableView
{
    if (!_searchPageTableView)
    {
        _searchPageTableView = [[UITableView alloc]initWithFrame:self.view.frame];
        _searchPageTableView.delegate = self;
        _searchPageTableView.dataSource = self;
        
    }
    return _searchPageTableView;
}
- (NSMutableArray *)sessionArr
{
    if (!_sessionArr)
    {
        _sessionArr = [[NSMutableArray alloc]init];
        
    }
    return _sessionArr;
}
- (NSMutableArray *)cellArr
{
    if (!_cellArr)
    {
        _cellArr = [[NSMutableArray alloc]init];
    }
    return _cellArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
